package com.eliza.base.views.learn.widget

import android.content.Context
import androidx.glance.GlanceId
import androidx.glance.action.ActionParameters
import androidx.glance.appwidget.action.ActionCallback
import androidx.glance.appwidget.state.updateAppWidgetState


//系统会调用所提供的 ActionCallback 的 suspend onAction 方法，执行定义的逻辑（即请求刷新数据）。
class RefreshAction : ActionCallback {
    override suspend fun onAction(
        context: Context,
        glanceId: GlanceId,
        parameters: ActionParameters
    ) {
        // do some work but offset long-term tasks (e.g a Worker)
        MyAppWidget().update(context, glanceId)
    }
}

class ClickAction : ActionCallback {
    override suspend fun onAction(
        context: Context,
        glanceId: GlanceId,
        parameters: ActionParameters
    ) {
        // Get the current state of the given widget and the value provided in the ActionParameters
        updateAppWidgetState(context, glanceId) { state ->
            state[CountClicksKey] = (state[CountClicksKey] ?: 0) + (parameters[ClickValueKey] ?: 0)
        }
        // Trigger the widget update
        MyAppWidget().update(context, glanceId)
    }
}